//
//  DrawerViewController.m
//  HLZhiHuDaily
//
//  Created by 胡林 on 16/3/1.
//  Copyright © 2016年 胡林. All rights reserved.
//

#import "DrawerViewController.h"
#import "SYView.h"
#import "leftModel.h"
#import "DailyThemesViewController.h"
#import "MainViewController.h"
#import "loginViewController.h"
@interface DrawerViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong,nonatomic) NSMutableArray *subscribedList;
@property (strong,nonatomic) NSMutableArray *othersList;
@property (strong,nonatomic) NSMutableArray *themeItems;

@property (nonatomic ,strong)MainViewController * mainViewC;

@property(nonatomic,assign)BOOL isNigth;
@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getThemeList];
//    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
//    [self.nigth addGestureRecognizer:tap];
//    [self.nigth addTarget:self action:@selector(tapAction) forControlEvents:(UIControlEventTouchUpInside)];
//    self.isNigth = NO;
    

}
- (IBAction)nigth:(id)sender {
    
    
}

//UINavigationController * naC = [[UINavigationController alloc] initWithRootViewController:rootVC];

- (IBAction)log:(id)sender {
    loginViewController * login = [[loginViewController alloc] init];
    UINavigationController * loginNaC = [[UINavigationController alloc] initWithRootViewController:login];
    [self presentViewController:loginNaC animated:YES completion:nil];
//    [self.navigationController popViewControllerAnimated:YES];
}
//-(void)tapAction{
//    if (self.isNigth == NO) {
//        self.mainViewC.view.backgroundColor = [UIColor whiteColor];
//    }else{
//        self.mainViewC.view.backgroundColor = [UIColor blackColor];
//    }
//}
//- (IBAction)niget:(id)sender {
//
//        if (self.isNigth == NO) {
//            self.mainViewC.view.backgroundColor = [UIColor whiteColor];
//            self.isNigth = YES;
//        }else{
//            self.mainViewC.view.backgroundColor =[UIColor blackColor];
//            self.isNigth = NO;
//        }
//}


//侧滑栏的内容
-(void)getThemeList{
  
    [RequestURL getRequestWithUrl:@"themes" parameters:nil success:^(id responseObject) {
        NSDictionary * json = (NSDictionary *)responseObject;
        _subscribedList = [NSMutableArray array];
        NSArray * subA = json[@"subscribed"];
        for (NSDictionary * dic in subA) {
            leftModel * model = [[leftModel alloc] initWithDictionary:dic];
            [_subscribedList addObject:model];
        }
        _othersList = [NSMutableArray array];
        NSArray * othA = json[@"others"];
        for (NSDictionary * dic in othA) {
            leftModel * model =[[leftModel alloc] initWithDictionary:dic];
            [_othersList addObject:model];
        }
        [self setMentItems];
    } failure:nil];
    
    
}
//侧滑栏显示的内容
-(void)setMentItems{
    self.scrollView.contentSize = CGSizeMake(0, (1+self.subscribedList.count + self.othersList.count) * 44.f);
    SYView * syV =[[[NSBundle mainBundle] loadNibNamed:@"SYView" owner:self options:nil] firstObject];
    syV.frame = CGRectMake(0, 0, self.scrollView.width, 44);
    syV.meunLabel.text = @"首页";
    syV.meunImage.image = [UIImage imageNamed:@"Menu_Icon_Home"];
    [syV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHome:)]];
    [self.scrollView addSubview:syV];
    CGFloat temoHeight = syV.height;
    NSMutableArray * theme = [NSMutableArray arrayWithArray:self.subscribedList];
    [theme addObjectsFromArray:self.othersList];
    for (int i = 0; i < theme.count; i++) {
        SYView * itenV = [[[NSBundle mainBundle] loadNibNamed:@"SYView" owner:self options:nil] firstObject];
        itenV.frame = CGRectMake(0, temoHeight, self.scrollView.width, 44);
        //把图片删除
        [itenV.meunImage removeFromSuperview];
        [itenV addConstraint:[NSLayoutConstraint constraintWithItem:itenV.meunLabel attribute:(NSLayoutAttributeLeading) relatedBy:(NSLayoutRelationEqual) toItem:itenV attribute:(NSLayoutAttributeLeading) multiplier:1 constant:4]];
        leftModel * model = theme[i];
        itenV.meunLabel.text = model.name;
        [itenV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedMenuItem:)]];
        [itenV setTag:i];
        [self.scrollView addSubview:itenV];
        temoHeight += 44.f;
    }
    self.themeItems = theme;
}


//返回首页
- (void)showHome:(UIGestureRecognizer *)recognizer{
    AppDelegate *app = kAppdelegate;
    [app.mainVC showMainView];
    }
//侧滑栏ID1

- (void)didSelectedMenuItem:(UIGestureRecognizer *)recognizer{
    leftModel * model = self.themeItems[recognizer.view.tag];
    DailyThemesViewController * dailyTVC = [[DailyThemesViewController alloc] init];
    dailyTVC.themeID = model.themeID;
    UINavigationController * subNavC = [[UINavigationController alloc] initWithRootViewController:dailyTVC];
    AppDelegate * app = kAppdelegate;
    [app.mainVC presentViewController:subNavC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
